package com.newtouch.bxzs.business.statistics.service.impl;

import cn.hutool.core.date.DateUtil;
import com.newtouch.bxzs.business.statistics.mapper.IncomeMapper;
import com.newtouch.bxzs.business.statistics.model.bo.*;
import com.newtouch.bxzs.business.statistics.model.vo.AchievementSummaryVO;
import com.newtouch.bxzs.business.statistics.model.vo.EstimateIncomeVO;
import com.newtouch.bxzs.business.statistics.model.vo.PersonalIncomeCompositionVO;
import com.newtouch.bxzs.business.statistics.service.AchievementService;
import com.newtouch.bxzs.business.statistics.service.PersonalIncomeService;
import com.newtouch.bxzs.common.base.BaseErrorEnum;
import com.newtouch.bxzs.common.base.BaseException;
import com.newtouch.bxzs.common.base.em.DistributionTypeEnum;
import com.newtouch.bxzs.common.base.em.SystemParamsEnum;
import com.newtouch.bxzs.common.base.util.RedisUtils;
import com.newtouch.bxzs.common.base.util.UserDetailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 收入服务实现类
 *
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021/1/13</td>
 * <td>tianshangzhu</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author tianshangzhu
 * @version 1.0.0
 * @since 1.0.0
 */
@Service
public class PersonalIncomeServiceImpl implements PersonalIncomeService {
    /**
     * 导入收入mapper
     */
    @Resource
    private IncomeMapper incomeMapper;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    AchievementService achievementService;

    /**
     * 配置的最大月份、最小月份、一年的月份
     */
    @Value("${monthLimit.maxMonth}")
    private Integer maxMonth;
    @Value("${monthLimit.minMonth}")
    private Integer minMonth;
    @Value("${monthLimit.oneYear}")
    private Integer oneYear;

    /**
     * (non-Javadoc)
     * <p>
     * <p>
     * com.newtouch.bxzs.business.statistics.service.impl.PersonalIncomeServiceImpl#getPersonalIncome(com.newtouch.bxzs.business.statistics.model.vo.AchievementSummaryVO)
     */
    @Override
    public List<PersonalIncomeBO> getPersonalIncome(AchievementSummaryVO income) {
        /* 判断必填内容（页数、条数、开始月份、结束月份） */
        if (income.getStartMonth() == null
                || income.getEndMonth() == null) {
            throw new BaseException(BaseErrorEnum.REQUEST_DATA_NULL);
        }else {
            income.setStartMonth(DateUtil.beginOfMonth(income.getStartMonth()));
            income.setEndMonth(DateUtil.endOfMonth(income.getEndMonth()));
            achievementService.decideDateRange(income.getStartMonth(),income.getEndMonth());
        }
        if (income.getSortType()){
            return incomeMapper.getPersonalIncomeByCompany(income,UserDetailUtil.getUserDetail().getUserJobNumber());
        }else {
            return incomeMapper.getPersonalIncomeByMonth(income,UserDetailUtil.getUserDetail().getUserJobNumber());
        }
    }

    /**
     * (non-Javadoc)
     * <p>
     * <p>
     * com.newtouch.bxzs.business.statistics.service.impl.PersonalIncomeServiceImpl#getPersonalIncomeComposition(com.newtouch.bxzs.business.statistics.model.vo.AchievementSummaryVO)
     */
    @Override
    public List<PersonalIncomeCompositionBO> getPersonalIncomeComposition(PersonalIncomeCompositionVO income) {
        if (DistributionTypeEnum.BANK.getCode().equals(redisUtils.get(SystemParamsEnum.BANKING_OR_INSURANCE.getCode()))){
            if (income.getMonth() == null && StringUtils.isEmpty(income.getOrganizationCode())){
                throw new BaseException(BaseErrorEnum.REQUEST_DATA_NULL);
            }else if (income.getMonth() != null){
                income.setMonth(DateUtil.beginOfMonth(income.getMonth()));
            }else if (income.getMonth() == null){
                income.setStartMonth(DateUtil.beginOfMonth(income.getStartMonth()));
                income.setEndMonth(DateUtil.endOfMonth(income.getEndMonth()));
            }
            if (income.getMonth() != null){
                return incomeMapper.getPersonalIncomeCompositionByMonth(income,UserDetailUtil.getUserDetail().getUserJobNumber());
            }else {
                return incomeMapper.getPersonalIncomeCompositionByCompany(income,UserDetailUtil.getUserDetail().getUserJobNumber());
            }
        }else {
            if (income.getMonth() == null){
                throw new BaseException("月份不能为空");
            }else {
                income.setMonth(DateUtil.beginOfMonth(income.getMonth()));
            }
            return incomeMapper.getPersonalIncomeCompositionByInsurance(income,UserDetailUtil.getUserDetail().getUserJobNumber());
        }

    }


    /**
     * (non-Javadoc)
     * <p>
     * <p>
     * com.newtouch.bxzs.business.statistics.service.impl.PersonalIncomeServiceImpl#getPolicyIncomeDetail(com.newtouch.bxzs.business.statistics.model.vo.PersonalIncomeCompositionVO)
     */
    @Override
    public List<IncomePolicyDetailBO> getPolicyIncomeDetail(PersonalIncomeCompositionVO income) {
        if (income.getMonth() == null && StringUtils.isEmpty(income.getOrganizationCode())){
            throw new BaseException(BaseErrorEnum.REQUEST_DATA_NULL);
        }else if (income.getMonth() != null && !StringUtils.isEmpty(income.getOrganizationCode())){
            throw new BaseException("不能同时根据月份和机构查询收入明细");
        }else if (income.getMonth() != null){
            income.setMonth(DateUtil.beginOfMonth(income.getMonth()));
        }
        if (income.getAgencyFlg() == null){
            throw new BaseException(BaseErrorEnum.REQUEST_DATA_NULL);
        }
        if(income.getAgencyFlg()){
            incomeMapper.getPolicyIncomeDetail(income,UserDetailUtil.getUserDetail().getUserJobNumber());
        }
        return incomeMapper.getPolicyIncomeDetailByInsurance(income,UserDetailUtil.getUserDetail().getUserJobNumber());
    }

    /**
     * (non-Javadoc)
     * <p>
     * <p>
     * com.newtouch.bxzs.business.statistics.service.impl.PersonalIncomeServiceImpl#getEstimateIncomeSummary(com.newtouch.bxzs.business.statistics.model.vo.EstimateIncomeVO)
     */
    @Override
    public List<EstimateIncomeBO> getEstimateIncomeSummary(EstimateIncomeVO estimateIncomeVO) {
        EstimateIncomeVO estimateIncome = this.setMonth(estimateIncomeVO);
        if ((DistributionTypeEnum.INSURANCE.getCode()).equals(redisUtils.get(SystemParamsEnum.BANKING_OR_INSURANCE.getCode()))){
            return incomeMapper.getEstimateIncomeSummaryByInsurance(estimateIncome,UserDetailUtil.getUserDetail().getUserJobNumber());
        }
        return incomeMapper.getEstimateIncomeSummaryByBank(estimateIncome,UserDetailUtil.getUserDetail().getUserJobNumber());
    }

    /**
     * (non-Javadoc)
     * <p>
     * <p>
     * com.newtouch.bxzs.business.statistics.service.impl.PersonalIncomeServiceImpl#getEstimateIncomeDetail(com.newtouch.bxzs.business.statistics.model.vo.EstimateIncomeVO)
     */
    @Override
    public List<EstimateIncomeDetailBO> getEstimateIncomeDetail(EstimateIncomeVO estimateIncomeVO) {
        EstimateIncomeVO estimateIncome = this.setMonth(estimateIncomeVO);

        if ((DistributionTypeEnum.INSURANCE.getCode()).equals(redisUtils.get(SystemParamsEnum.BANKING_OR_INSURANCE.getCode()))){
            return incomeMapper.getEstimateIncomeDetailByInsurance(estimateIncome, UserDetailUtil.getUserDetail().getUserJobNumber());
        }else {
            if (StringUtils.isEmpty(estimateIncomeVO.getCompanyCode())){
                throw new BaseException(BaseErrorEnum.REQUEST_DATA_NULL);
            }
            return incomeMapper.getEstimateIncomeDetailByBank(estimateIncome, UserDetailUtil.getUserDetail().getUserJobNumber());
        }
    }

    /**
     * 预估收入赋值月初和当前时间
     * @return
     */
    public EstimateIncomeVO setMonth(EstimateIncomeVO estimateIncome){
        if (estimateIncome == null){
            throw new BaseException(BaseErrorEnum.REQUEST_DATA_NULL);
        }
        Date nowDate = DateUtil.parseDate(DateUtil.today());
        estimateIncome.setStartTime(DateUtil.beginOfMonth(nowDate));
        estimateIncome.setEndTime(nowDate);
        return estimateIncome;
    }
}
